#!/usr/bin/env python
# $Id: test2-32.py df54dec975f4 2012/09/09 20:22:40 pgurumur $
# Copyright (c) 2012 Prabhu Gurumurthy <pgurumur@gmail.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

__version__  = "$Revision: df54dec975f4 $"
__author__   = "$Author: pgurumur $"
__modified__ = "$Date: 2012-09-09 20:22:40Z $"

import getpass
import optparse
import os
import sys

from venya.lib import crypto
from venya.lib import cmdparser
from venya.lib import exception
from venya.lib import stdlib


class Crypto(cmdparser.GetOpt):
   option_list = (
         optparse.make_option("-f", "--filename", type = "str",
            help = "File to be encrypted", dest = "infile"),
         optparse.make_option("-a", "--algorithm", type = "choice",
            choices = ["aes", "blowfish", "3des"], dest = "algorithm"),
         optparse.make_option("-s", "--string", type = "str",
            help = "String to be encrypted", dest = "string"),
         optparse.make_option("-e", "--encrypt", action = "store_true",
            help = "Encrypt the string/file", default = False,
            dest = "encrypt"),
         ) + cmdparser.GetOpt.option_list

   def __init__(self, **kwargs):
      super(Crypto, self).__init__(**kwargs)

   def handle(self):
      (options, args) = self.parse()


      cipher = None
      if options.algorithm:
         if options.algorithm == "aes":
            cipher = crypto.aes(getpass.getpass("Enter secret: "))
         elif options.algorithm == "blowfish":
            cipher = crypto.blowfish(getpass.getpass("Enter secret: "))
         elif options.algorithm == "3des":
            cipher = crypto.tripledes(getpass.getpass("Enter secret: "))
         else:
            self.error("Unknown algorithm!")

      if options.infile and os.path.exists(options.infile):
         fh = None
         try:
            fh = open(options.infile, "r")
         except IOError as err:
            self.error(err)
         else:
            if options.encrypt:
               print(cipher.encrypt(fh.read()))
            else:
               print(cipher.decrypt(fh.read()))

            fh.close()
      elif options.string:
         if options.encrypt:
            print(cipher.encrypt(options.string))
         else:
            print(cipher.decrypt(options.string))

               
if __name__ == "__main__":
   cipher = None
   try:
      cipher = Crypto(version = 1.0, args = sys.argv)
      if len(cipher) <= 1:
         cipher.printhelp()
      else:
         cipher.handle()
   except exception.GetOptError as err:
      print(err)
      sys.exit(1)
